package com.qdsg.ysg.doctor.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;


/**
 * Created by HP on 2018/10/5.
 */

public class AddGroupDialog extends BaseDialogFragment {

    public static AddGroupDialog newInstance() {
        Bundle args = new Bundle();
        AddGroupDialog fragment = new AddGroupDialog();
        fragment.setArguments(args);
        return fragment;
    }

    private Handler mHandler = new Handler();
    int second = 60;

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView btn_confirm, btn_cancel, tv_message, btn_code;
    private EditText edt_code;

    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_add_group, container);
        btn_confirm = view.findViewById(R.id.btn_confirm);
        btn_cancel = view.findViewById(R.id.btn_cancel);
        edt_code = view.findViewById(R.id.edt_code);

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCancel();
                    dismiss();
                }
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    addGroup();
                }
            }
        });
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String code);

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    private void addGroup() {
        if (TextUtils.isEmpty(edt_code.getText().toString())) {
            return;
        }
        RestProxy.getInstance().addGroup(edt_code.getText().toString(), new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                mListener.onConfirm(edt_code.getText().toString());
                dismiss();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
